﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using Resto.CostControl.Core.Models.SysSettings;
using System.Data;
using Resto.CostControl.Core.Models;

namespace Resto.CostControl.Core
{
    public class CostContext : DbContext
    {
        public CostContext()
            : base("Cost")
        {
            var context = ((IObjectContextAdapter)this).ObjectContext;
            context.SavingChanges += new EventHandler(context_SavingChanges);
        }


        public DbSet<Material> Materials { get; set; }
        public DbSet<EnumItem> EnumItems { get; set; }
        public DbSet<EnumType> EnumTypes { get; set; }
        public DbSet<ProjectSite> Sites { get; set; }
        public DbSet<MaterialPrice> Prices { get; set; }
        public DbSet<Supplier> Suppliers { get; set; }
        public DbSet<Dish> Dishes { get; set; }
        public DbSet<DishMaterial> DishMaterials { get; set; }
        public DbSet<WeekDishOrder> WeekDishOrders { get; set; }
        public DbSet<DishOrderItem> DishOrderItems { get; set; }
        public DbSet<TempDishOrder> TempDishOrders { get; set; }
        public DbSet<OrderMaterial> OrderMaterials { get; set; }
        public DbSet<OrderReported> OrderReporteds { get; set; }
        public DbSet<OrderDish> OrderDishes { get; set; }
        public DbSet<OrderDateAndCategory> OrderDateAndCategorys { get; set; }

        public DbSet<MaterialPurchase> MaterialPurchases { get; set; }
        public DbSet<MaterialPurchaseItem> MaterialPurchaseItems { get; set; }

        public DbSet<Storage> Storages { get; set; }

        public DbSet<MaterialProcess> MaterialProcesses { get; set; }
        public DbSet<MaterialProcessItem> MaterialProcessItems { get; set; }

        public DbSet<DishDelivery> DishDeliveries { get; set; }
        public DbSet<DishDeliveryItem> DishDeliveryItems { get; set; }

        public DbSet<DishRange> DishRanges { get; set; }

        public DbSet<MaterialPurchasePlan> MaterialPurchasePlans { get; set; }
        public DbSet<MaterialProcessPlan> MaterialProcessPlans { get; set; }
        public DbSet<MaterialDeliveryPlan> MaterialDeliveryPlans { get; set; }

        public DbSet<Delivery> Deliveries { get; set; }
        public DbSet<DeliveryItem> DeliveryItems { get; set; }
        public DbSet<StockRecord> StockRecords { get; set; }
        #region 系统设置
        public DbSet<Privilege> Privileges { get; set; }
        public DbSet<RoleInfo> RoleInfos { get; set; }
        public DbSet<UserInfo> UserInfos { get; set; }
        public DbSet<Organization> Organizations { get; set; }
        public DbSet<log> logs { get; set; }
        public DbSet<DictInfo> DictInfos { get; set; }
        public DbSet<RoleUserRelation> RoleUserRelations { get; set; }
        public DbSet<V_RoleUserRelation> V_RoleUserRelations { get; set; }
        public DbSet<RolePrivilegeRelation> RolePrivilegeRelations { get; set; }
        #endregion

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // 不生成EdmMetadata表，也就不会检查Model和数据库的变更了
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

            modelBuilder.Entity<Dish>().Ignore(d => d.Items);
            modelBuilder.Entity<WeekDishOrder>().Ignore(d => d.MON);
            modelBuilder.Entity<TempDishOrder>().Ignore(d => d.Details);

            modelBuilder.Entity<MaterialPurchase>().Ignore(d => d.Items);
            modelBuilder.Entity<MaterialProcess>().Ignore(d => d.Items);
            modelBuilder.Entity<DishDelivery>().Ignore(d => d.Items);
            modelBuilder.Entity<Delivery>().Ignore(d => d.Items);
            #region 系统设置
            modelBuilder.Entity<Privilege>().Ignore(d => d.ParentName);
            modelBuilder.Entity<Organization>().Ignore(d => d.ParentName);
            modelBuilder.Entity<Organization>().Ignore(d => d.UnitName);
            modelBuilder.Entity<EnumItem>().Ignore(d => d.ParentName);
            #endregion

            base.OnModelCreating(modelBuilder);
        }

        void context_SavingChanges(object sender, EventArgs e)
        {
            var ObjectStateManager = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager;
            var updatedEntites = ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
            foreach (var updateEntity in updatedEntites)
            {
                var baseEntity = updateEntity.Entity as BaseModel;
                if (baseEntity != null)
                {
                    baseEntity.UpdateDate = DateTime.Now;
                    baseEntity.UpdateUser = "管理员";
                    baseEntity.UpdateUserId = "admin";

                    updateEntity.SetModifiedProperty("UpdateDate");
                    updateEntity.SetModifiedProperty("UpdateUser");
                    updateEntity.SetModifiedProperty("UpdateUserId");
                }
            }

            var insertEntites = ObjectStateManager.GetObjectStateEntries(EntityState.Added);
            foreach (var insertEntity in insertEntites)
            {
                var baseEntity = insertEntity.Entity as BaseModel;
                if (baseEntity != null)
                {
                    baseEntity.UpdateDate = DateTime.Now;
                    baseEntity.UpdateUser = "管理员";
                    baseEntity.UpdateUserId = "admin";

                    baseEntity.CreateDate = DateTime.Now;
                    baseEntity.CreateUser = "管理员";
                    baseEntity.CreateUserId = "admin";
                }
            }
        }
    }
}
